package co2.bean;

import java.io.Serializable;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.pojos.BankMotivasi;
import co2.pojos.BankSoal;
import co2.pojos.PaketMotivasi;
import co2.pojos.PaketSoal;

@ManagedBean
@ViewScoped
public class BankMotivasiBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private PaketMotivasi paketSoal;
	private BankMotivasi bankSoal;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	private SignInBean signInBean;
	private boolean editFlag;
	
	@PostConstruct
	public void postCon(){
		
		try{
			Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			signInBean = (SignInBean) map.get("signInBean");
			paketSoal = (PaketMotivasi) signInBean.getMap().get("paketSoalMotivasi");
			
			editFlag = signInBean.getMap().get("editFlag") != null;
			if(editFlag){
				bankSoal = (BankMotivasi) signInBean.getMap().get("bankM");
			}else{
				bankSoal = new BankMotivasi();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	public void update(){
		try {
			PaketMotivasi ps = new PaketMotivasi();
			ps.setId(paketSoal.getId());
			bankSoal.setPaketMotivasi(ps);
			dao.update(bankSoal);
			signInBean.getMap().remove("editFlag");
			signInBean.getMap().remove("bankM");

			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/list_bank_motivasi.jsf");
		} catch (Exception e) {
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Update",""));
		}
	}
	public void saveBank(){
		try{
			String id = (String) (new UUIDHexGenerator()).generate(null, null);
			bankSoal.setId(id.toUpperCase());
			PaketMotivasi ps = new PaketMotivasi();
			ps.setId(paketSoal.getId());
			bankSoal.setPaketMotivasi(ps);
			dao.save(bankSoal);
			bankSoal = new BankMotivasi();

			int jmlSoal = paketSoal.getJumlah();
			jmlSoal++;
			paketSoal.setJumlah(jmlSoal);
			dao.update(paketSoal);

			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/list_bank_motivasi.jsf");
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Saving", ""));
		}
	}
 
	public boolean isEditFlag() {
		return editFlag;
	}

	public void setEditFlag(boolean editFlag) {
		this.editFlag = editFlag;
	}
	public DAOInterface getDao() {
		return dao;
	}
	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
	public SignInBean getSignInBean() {
		return signInBean;
	}
	public void setSignInBean(SignInBean signInBean) {
		this.signInBean = signInBean;
	}
	public PaketMotivasi getPaketSoal() {
		return paketSoal;
	}
	public void setPaketSoal(PaketMotivasi paketSoal) {
		this.paketSoal = paketSoal;
	}
	public BankMotivasi getBankSoal() {
		return bankSoal;
	}
	public void setBankSoal(BankMotivasi bankSoal) {
		this.bankSoal = bankSoal;
	}

}